package com.gxa.p2p.controller;

import com.gxa.p2p.businessExceptionCode.ErrorCode;
import com.gxa.p2p.exception.BusinessException;
import com.gxa.p2p.pojo.Menus;
import com.gxa.p2p.pojo.SysUser;
import com.gxa.p2p.service.MenuListService;
import com.gxa.p2p.service.SysUserService;
import com.gxa.p2p.util.JWTUtil;
import com.gxa.p2p.util.MapUtil;
import io.swagger.annotations.*;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.net.UnknownHostException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.UUID;

/**
 * 系统用户控制器
 *
 * @author Longlin_Feng
 * @date 2020/8/2
 */
@Controller
@CrossOrigin
@RequestMapping("/sysUser")
@Api(value = "系统用户控制器", tags = "系统用户管理接口")
public class SysUserController {

  @Resource(name = "sysService")
  private SysUserService sysUserService;

  @Resource private MenuListService menuListService;

  @Resource private HttpServletRequest request;

  /**
   * 添加系统用户
   *
   * @param sysUser 封装的用户实例
   * @return Map<String , Object> 封装成json的后端信息
   */
  @PostMapping("/add")
  @ResponseBody
  @ApiOperation(value = "系统用户添加接口", httpMethod = "post", response = Map.class)
  public Map<String, Object> addSysUser(
      @RequestBody @ApiParam(name = "sysUser", value = ("系统用户对象"), required = true)
          SysUser sysUser) {

    if (sysUserService.add(sysUser)) {
      // 调用添加方法,添加成功添加状态码0,否则抛出异常
      MapUtil mapUtil = new MapUtil();
        mapUtil.setCode(0);

      return mapUtil.getMap();
    } else {
      throw new BusinessException(ErrorCode.FAILED);
    }
  }

  /**
   * 移除用户
   *
   * @param sysUser 要移除的用户
   * @return Map<String , Object> 封装成json的后端信息
   */
  @DeleteMapping("/remove")
  @ResponseBody
  @ApiOperation(value = "系统用户移除接口", httpMethod = "delete", response = Map.class)
  @ApiImplicitParam(name = "sysUser", value = "系统用户对象", required = true, paramType = "SysUser")
  public Map<String, Object> removeSysUser(@RequestBody SysUser sysUser) {

    System.out.println("id:  " + sysUser.getId());
    // 调用移除系统用户接口
    if (sysUserService.remove(sysUser.getId())) {

      MapUtil mapUtil = new MapUtil();
        mapUtil.setCode(0);
      return mapUtil.getMap();
    } else {
      throw new BusinessException(ErrorCode.FAILED);
    }
  }

  /**
   * 根据要删除的系统用户的id列表批量删除
   *
   * @param idList 要批量删除用户的id列表
   * @return Map<String , Object> 封装成json的后端信息
   */
  @DeleteMapping("/batchRemove")
  @ResponseBody
  @ApiOperation(value = "系统用户批量移除接口", httpMethod = "delete", response = Map.class)
  @ApiImplicitParam(name = "idList", value = "要删除系统用户的id组成的列表", required = true, paramType = "List")
  public Map<String, Object> removeSysUsersByBatch(@RequestBody List<Integer> idList) {

    // 调用批量删除接口
    if (sysUserService.removeByBatch(idList)) {

      MapUtil mapUtil = new MapUtil();
        mapUtil.setCode(0);
      return mapUtil.getMap();
    } else {
      throw new BusinessException(ErrorCode.FAILED);
    }
  }

  /**
   * 系统用户信息修改接口
   *
   * @param sysUser 修改后的目标信息封装的系统用户
   * @return Map<String , Object> 封装成json的后端信息
   */
  @PutMapping("/edit")
  @ResponseBody
  @ApiOperation(value = "系统用户信息修改接口", httpMethod = "put", response = Map.class)
  @ApiImplicitParam(
      name = "sysUser",
      value = "修改后的目标信息封装的系统用户",
      required = true,
      paramType = "SysUser")
  public Map editSysUserInfo(@RequestBody SysUser sysUser) {

    if (sysUserService.edit(sysUser)) {

      MapUtil mapUtil = new MapUtil();
        mapUtil.setCode(0);
      return mapUtil.getMap();
    } else {
      throw new BusinessException(ErrorCode.FAILED);
    }
  }

  /**
   * 系统用户分页查询接口
   *
   * @param paramMap 参数map:包含分页的page和limit,以及查询条件(工号/姓名/手机/用户类型ID,没有则为全查询)
   * @return List<SysUser> 目标记录
   */
  @PostMapping("/findByPage")
  @ResponseBody
  @ApiOperation(
      value = "系统用户分页查询(可带入条件:工号/姓名/手机/用户类型ID)接口",
      httpMethod = "post",
      response = List.class)
  @ApiImplicitParam(
      name = "paramMap",
      value = "参数map:包含分页的page和limit,以及查询条件(没有则为全查询)",
      required = true,
      dataType = "Map")
  public Map<String, Object> findSysUsersByPage(@RequestBody Map<String, Object> paramMap) {
    // 调用查询接口
    List<SysUser> sysUserList = sysUserService.findByPage(paramMap);
    // 如果返回列表的大于0,计数并返回列表和总数
    if (sysUserList.size() > 0) {

      MapUtil mapUtil = new MapUtil();
        mapUtil.setCode(0);
      mapUtil.put("sysUserList", sysUserList);
      mapUtil.put("count", sysUserService.getCount(paramMap));

      return mapUtil.getMap();
    } else {
      throw new BusinessException(ErrorCode.FAILED);
    }
  }

  /**
   * 系统用户登录接口
   *
   * @param sysUser jobNumber和password 封装的用户对象
   * @return Map<String , Object> 封装成json的后端信息
   */
  @PostMapping("/login")
  @ResponseBody
  @ApiOperation(value = "系统用户登录接口(包含菜单管理)", httpMethod = "post", response = Map.class)
  @ApiImplicitParams({
    @ApiImplicitParam(name = "jobNumber", value = "工号", required = true, paramType = "String"),
    @ApiImplicitParam(name = "password", value = "密码", required = true, paramType = "String")
  })
  public Map<String, Object> login(@RequestBody SysUser sysUser) throws UnknownHostException {
    // 获取发起请求的ip
    String remoteAddr = request.getRemoteAddr();
    // 调用service层登录接口,获取返回的sysUser
    SysUser sysUserLogin =
        sysUserService.login(sysUser.getJobNumber(), sysUser.getPassword(), remoteAddr);

    if (sysUserLogin != null) {
      JWTUtil jwtUtil = new JWTUtil();
      String token =
          jwtUtil.createJWT(
              UUID.randomUUID().toString().replace("-", ""),
                  sysUser.getJobNumber(),
              30 * 60 * 1000,
              new HashMap<>());

      // 根据权限获取菜单列表,roleId从返回的sysUser中获得
      List<Menus> menuList = menuListService.getMenuList(sysUserLogin.getRoleId());
      MapUtil mapUtil = new MapUtil();
        mapUtil.setCode(0);
      mapUtil.put("sysUserLogin", sysUserLogin);
      mapUtil.put("token", token);
      mapUtil.put("menuInfo", menuList);
      return mapUtil.getMap();
    } else {
      throw new BusinessException(ErrorCode.FAILED);
    }
  }

  /**
   * 全查询,用于导出数据
   *
   * @return List<SysUser>
   */
  @PostMapping("/all")
  @ResponseBody
  @ApiOperation(value = "全查询接口,用于导出数据", httpMethod = "post", response = List.class)
  @ApiImplicitParam(
      name = "paramMap",
      value = "参数map:查询条件(可以为空)",
      required = true,
      dataType = "Map")
  public Map<String, Object> findAll(@RequestBody Map<String, Object> paramMap) {
    // 调用服务层接口
    List<SysUser> all = sysUserService.findAll(paramMap);
    MapUtil mapUtil = new MapUtil();
      mapUtil.setCode(0);
    mapUtil.put("sysUserList", all);
    mapUtil.put("count", sysUserService.getCount(paramMap));

    return mapUtil.getMap();
  }

  /**
   * 系统用户职务变更
   *
   * @param sysUser 职务变更后的目标系统用户对象
   * @return Map<String , Object> 封装成json的后端信息
   */
  @PutMapping("/positionChange")
  @ResponseBody
  @ApiOperation(value = "系统用户职务变更接口", httpMethod = "put", response = Map.class)
  @ApiImplicitParam(
      name = "sysUser",
      value = "职务变更后的目标系统用户",
      required = true,
      paramType = "SysUser")
  public Map<String, Object> positionChange(@RequestBody SysUser sysUser) {
    // 调用服务层变更职务接口
    if (sysUserService.positionChange(sysUser)) {

      MapUtil mapUtil = new MapUtil();
        mapUtil.setCode(0);
      return mapUtil.getMap();
    } else {
      throw new BusinessException(ErrorCode.FAILED);
    }
  }
}
